﻿using System;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
using Fuse8.DomainFramework.BusinessService;

namespace Fuse8.ContentManagementFramework.Domain.Special
{
    [Serializable]
    public abstract class CmsRevisionableEntityObject : CmsLocalizedEntityObject, IRevisionableEntityObject, IWorkflowEntityObject<int>
    {
        private CmsWorkflow _workflow;

        public virtual bool? IsLatestRevision { get; set; }

        public virtual bool? IsLatestCompletedRevision { get; set; }

        public virtual bool? IsDeleted { get; set; }

        public virtual string WorkflowTitle { get; set; }

        public virtual Guid? WorkflowId { get; set; }

        [XmlIgnore]
        [ScriptIgnore]
        public virtual CmsWorkflow Workflow
        {
            get
            {
                if (_workflow == null)
                {
                    if (WorkflowId.HasValue && WorkflowId != Guid.Empty)
                    {
                        var businessFacade = new BusinessServiceFacade<CmsWorkflow, Guid>();
                        _workflow = businessFacade.FindOne(WorkflowId.Value);
                    }
                }

                return _workflow;
            }
            set
            {
                WorkflowId = value != null ? value.Id : new Nullable<Guid>();

                _workflow = value;
            }
        }
    }
}
